var app = getApp();
var nt_util = require('../../utils/nt_util.js')
Page({
  data: {
    mobile: '',
    validCode: '',
    sendBtn: false,
    phoneCode: '获取验证码',
    getSellerInfo: {},
    showValidCode:false, // 是否显示验证码
    showModal:false,
    imageCode:''
  },
  onLoad: function () {
    this.getDatas();
  },
  getDatas: function () {

    nt_util.httpRequest(nt_util.api.getSellerInfo, 'get', {}, function (data) {
      nt_util.that.setData({
        getSellerInfo: data.data
      });
    });
  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    nt_util.that = this;
  },



  sendsms: function () {
    // TODO 验证码倒计时
    var that = this;

    if (!nt_util.checkMobile(that.data.mobile)) {
      return
    }

    if (that.data.sendBtn) {
      return;
    }
    that.setData({
      sendBtn: true
    });

    nt_util.httpRequest(nt_util.api.sendSms, 'get', { 
      mobile: that.data.mobile,
      imageCode: that.data.imageCode
      }, function (res) {
      if (res.code == 100 || res.code == 0) {
        that.settime();
      } else {
        that.setData({
          sendBtn: false
        });
      }
    });
  },
  onLoginTap: function () {
    var that = this;
    if (!that.checkParams()) {
      return false;
    }

    var _param = {
      openid: nt_util.getOpenId(),
      mobile: that.data.mobile,
      name: that.data.name,
      validateCode: that.data.validCode
    };

    nt_util.httpRequest(nt_util.api.bindBuyer, 'post', _param, function (res) {
      if (res.code == 100 || res.code == 0) {
        wx.setStorage({
          key: "openId",
          data: res.data.buyer.openid
        })

        // 绑定成功后本地缓存手机号
        wx.setStorage({
          key: "mobile",
          data: res.data.buyer.mobile
        })

        wx.setStorage({
          key: "buyerId",
          data: res.data.buyer.id
        })

        wx.redirectTo({
          url: '/pages/index/index',
        })
      } else {
        nt_util.log(_param);
        nt_util.log(res);
        wx.showToast({
          image: '/img/kulian.png',
          title: res.msg,
        })
      }
    });
    
  },

  bindKeyInput: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var data_type = e.detail.value;
    switch (dataset.type) {
      case 'name':
        that.setData({
          name: data_type
        });
        break;
      case 'mobile':
        that.setData({
          mobile: data_type
        });
        break;
      case 'validCode':
        that.setData({
          validCode: data_type
        });
        break;
    }
  },

// 倒计时
  settime: function (val) {
    var that = this;
    that.setData({
      phoneCode: 60
    })
    let time = setInterval(() => {
      let phoneCode = that.data.phoneCode
      phoneCode--
      that.setData({
        phoneCode: phoneCode
      })
      if (phoneCode == 0) {
        clearInterval(time)
        that.setData({
          phoneCode: "获取验证码",
          sendBtn: false
        })
      }
    }, 1000)
  },
  checkParams: function () {
    var that = this;

    if (!that.data.name) {
      wx.showToast({
        title: '姓名不能为空',
      })
      return false;
    }
    if (!that.data.mobile) {
      wx.showToast({
        title: '手机号不能为空',
      })
      return false;
    }
    if (!that.data.validCode) {
      wx.showToast({
        title: '验证码不能为空',
      })
      return false;
    }
    return true;
  },

// 以下为图形验证码模态框
  showModalTap:function(){
    this.setData({ 
      showModal:true
      })
  },
  onConfirm:function(e){
    this.setData({
      imageCode: e.detail.imageCodeInput,
      showModal: false
    })
    this.sendsms()
  }

})

